﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;

namespace TestLib.MockDBTools
{
    public class MockDbParameterCollection:DbParameterCollection
    {
        List<MockDbParameter> pList = new List<MockDbParameter>();
        object root = new object();
        public MockDbParameterCollection()
        {
        }


        public override int Add(object value)
        {
            MockDbParameter mdp = CreateParameter(value);
            

            pList.Add(mdp);
            return pList.IndexOf(mdp);
        }

        private MockDbParameter CreateParameter(object value)
        {
            MockDbParameter mdp = new MockDbParameter();
            mdp.ParameterName = String.Concat("@param", (pList.Count + 1).ToString());
            mdp.Value = value;
return mdp;
        }

        public override void AddRange(Array values)
        {
            pList.ForEach(x=>Add(x));
        }

        public override void Clear()
        {
            pList.Clear();
        }

        public override bool Contains(string name)
        {
            return IndexOf(name) >= 0;
        }

        public override bool Contains(object value)
        {
            return IndexOf(value) >= 0;
        }

        public override void CopyTo(Array array, int index)
        {
            int i = index;
            pList.ForEach(x => array.SetValue(x,i++));
        }

        public override int Count
        {
            get { return pList.Count; }
        }

        public override System.Collections.IEnumerator GetEnumerator()
        {
            return pList.GetEnumerator();
        }

        protected override DbParameter GetParameter(string parameterName)
        {
            return pList.Find(x => x.ParameterName.Equals(parameterName, StringComparison.OrdinalIgnoreCase));
        }

        protected override DbParameter GetParameter(int index)
        {
            return pList[index];
        }

        public override int IndexOf(string parameterName)
        {
            return pList.FindIndex(x => x.ParameterName.Equals(parameterName, StringComparison.OrdinalIgnoreCase));
        }

        public override int IndexOf(object value)
        {
            return pList.FindIndex(x => x.Value.Equals(value));
        }

        public override void Insert(int index, object value)
        {
            MockDbParameter mdp = CreateParameter(value);

            pList.Insert(index, mdp);
        }

        public override bool IsFixedSize
        {
            get { return false; }
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override bool IsSynchronized
        {
            get { return true; }
        }

        public override void Remove(object value)
        {
            pList.RemoveAt(IndexOf(value));
        }

        public override void RemoveAt(string parameterName)
        {
            pList.RemoveAt(IndexOf(parameterName));
        }

        public override void RemoveAt(int index)
        {
            pList.RemoveAt(index);
        }

        protected override void SetParameter(string parameterName, DbParameter value)
        {
            int index = IndexOf(parameterName);
            if (index > 0)
            {
                pList[index].Value = value.Value;
            }
            else
            {
                MockDbParameter mdp = CreateParameter(value.Value);
                mdp.ParameterName = parameterName;
                pList.Add(mdp);
            }
        }

        protected override void SetParameter(int index, DbParameter value)
        {
            pList[index].Value = value.Value;
        }

        public override object SyncRoot
        {
            get { return root ; }
        }
    }
}
